package edu.spec.sms.controller;

import edu.spec.sms.model.Student;
import edu.spec.sms.model.Suggestion;
import edu.spec.sms.service.iface.ISuggestionService;
import edu.spec.sys.model.User;
import edu.spec.sys.model.dto.MvcReturn;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class SuggestionController {

    @Inject
    private ISuggestionService suggestionService;

    @ResponseBody
    @RequestMapping("/suggest/add")
    public MvcReturn add(String content, HttpSession session){
        Suggestion suggestion = new Suggestion();

        Object o = session.getAttribute("user");
        if(o instanceof User){
            suggestion.setUser((User)o);
        }else{
            suggestion.setStudent((Student)o);
        }
        suggestion.setContent(content);
        suggestion.setSuggestDate(new Date());
        suggestionService.add(suggestion);

        return new MvcReturn(true);
    }
}
